package org.schemaspy.input.dbms.driver;

import org.schemaspy.input.dbms.driverclass.Driverclass;
import org.schemaspy.input.dbms.exceptions.ConnectionFailure;

import java.sql.Driver;

/**
 * Encapsulates how to access a JDBC driver through class.
 */
public final class DsDriverClass implements Driversource {

    private final Driverclass driverclass;

    public DsDriverClass(final Driverclass driverclass) {
        this.driverclass = driverclass;
    }

    @Override
    public Driver driver() {
        try {
            // have to use deprecated method or we won't see messages generated by older drivers
            return this.driverclass.value().newInstance();
        } catch (Exception exc) {
            throw new ConnectionFailure(
                "Failed to create driver from driver class.", exc
            );
        }
    }
}
